package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {


    @Override
    public ResponseResult saveWmUser(WmUser wmUser) {
        if(wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmUser.setCreatedTime(new Date());
        boolean b = this.save(wmUser);
        if(b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public WmUser findByName(String name) {
        if(StringUtils.isBlank(name)){
            return null;
        }

//        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WmUser::getName,name);
//
//        QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
//        queryWrapper.lambda().eq(WmUser::getName,name);
//
//        Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name);

        List<WmUser> wmUserList = this.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if(wmUserList.size()>0){
            return  wmUserList.get(0);
        }
        return null;
    }
}
